package com.ithuameng.admin.app.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithuameng.admin.app.system.model.DictType;
import com.ithuameng.admin.app.system.model.admin.qo.DictTypeQo;
import com.ithuameng.admin.app.system.model.vo.AdminPageResVo;
import com.ithuameng.admin.app.system.model.vo.R;
import com.ithuameng.admin.app.system.service.IDictTypeService;
import com.ithuameng.admin.enums.CommonError;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 数据字典信息
 * 
 * @author ithuameng
 */
@Api(tags = "字典类型控制器")
@RestController
@RequestMapping("/admin/v1/dict/type")
public class DictTypeController extends AdminBaseController {

	@Resource
	private IDictTypeService dictTypeService;

	@ApiOperation("获取字典类型列表")
	@PreAuthorize("@ss.hasPermi('system:dict:list')")
	@GetMapping("/list")
	public R<AdminPageResVo<DictType>> list(DictTypeQo dictTypeQo) {
		Page<DictType> page = new Page<>(dictTypeQo.getPageNumber(), dictTypeQo.getPageSize());
		IPage<DictType> pageList = dictTypeService.listDictType(page, dictTypeQo);
		AdminPageResVo<DictType> dictTypePage = new AdminPageResVo<>();
		dictTypePage.setRecords(pageList.getRecords());
		dictTypePage.setPage(pageList.getPages());
		dictTypePage.setTotal(pageList.getTotal());
		return new R<>(dictTypePage);
	}

	@ApiOperation("查询字典类型详细")
	@PreAuthorize("@ss.hasPermi('system:dict:list')")
	@GetMapping(value = "/detail/{id}")
	public R<DictType> getInfo(@PathVariable Long id) {
		DictType dictTypeDO = dictTypeService.getById(id);
		return new R<>(dictTypeDO);
	}

	@ApiOperation("新增字典类型")
	@PreAuthorize("@ss.hasPermi('system:dict:add')")
	@PostMapping(value = "/add")
	public R<Boolean> add(@Validated @RequestBody DictType dict) {
		if (dict.getStatus() == null) {
			return new R<>(CommonError.BIZ_ERR, "请选择状态");
		}
		if (!dictTypeService.checkDictTypeUnique(dict)) {
			return new R<>(CommonError.BIZ_ERR, "字典类型已存在");
		}
		dictTypeService.save(dict);
		return new R<>(CommonError.OK, "添加成功");
	}

	@ApiOperation("修改字典类型")
	@PreAuthorize("@ss.hasPermi('system:dict:edit')")
	@PostMapping(value = "/edit")
	public R<Boolean> edit(@Validated @RequestBody DictType dict) {
		if (dict.getStatus() == null) {
			return new R<>(CommonError.BIZ_ERR, "请选择状态");
		}
		if (!dictTypeService.checkDictTypeUnique(dict)) {
			return new R<>(CommonError.BIZ_ERR, "字典类型已存在");
		}
		dictTypeService.updateById(dict);
		return new R<>(CommonError.OK, "修改成功");
	}

	@ApiOperation("删除字典类型")
	@PreAuthorize("@ss.hasPermi('system:dict:remove')")
	@PostMapping("/delete/{dictIds}")
	public R<Boolean> remove(@PathVariable Long[] dictIds) {
		dictTypeService.removeByIds(Arrays.asList(dictIds));
		return new R<>(CommonError.OK, "刪除成功");
	}

	@ApiOperation("刷新字典缓存")
	@PreAuthorize("@ss.hasPermi('system:dict:remove')")
	@PostMapping("/refreshCache")
	public R<Boolean> refreshCache() {
		dictTypeService.resetDictCache();
		return new R<>(CommonError.OK, "刷新缓存成功");
	}

	@ApiOperation("获取字典选择框列表")
	@GetMapping("/optionselect")
	public R<List<DictType>> optionselect() {
		List<DictType> dictTypes = dictTypeService.list();
		return new R<>(dictTypes);
	}
}
